home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / cuj1008.zip / 1008052A < prev    next >
Text File  |  1992-05-15  |  3KB  |  103 lines

  1. /*                    Listing 5                     */
  2. /*****************************************************
  3.                 Name: EXAMPLE.C
  4.          Description: Example program for ray / surface
  5.                       intercepts
  6.          Portability: Standard C, MSC with GRAPHICS_ON
  7. *****************************************************/
  8.  
  9. #include <stdio.h>
  10. #define GRAPHICS_ON
  11. #if defined( GRAPHICS_ON )
  12. #include <graph.h>
  13. #endif
  14. #include <matrix_t.h>
  15. #include <srf_trns.h>
  16. #include <srf_incp.h>
  17.  
  18. void main( void )
  19.    {
  20.    double T[3], Ip[3], DirH[3], DirV[3],
  21.       IpH[MAX_NUM_INCP], IpV[MAX_NUM_INCP],
  22.       Offset, MinH = 0.0, MaxH = 10.0, MinV = 0.0,
  23.       MaxV = 10.0, LocH[3], LocV[3], LocHAxis,
  24.       LocVAxis, StpHAxis, StpVAxis;
  25.    matrix_t C;
  26.    size_t i, NmSteps = 100;
  27.    int NmIpH = 0, NmIpV = 0, IpNm;
  28.  
  29.    /* Create a matrix and fill with coefficients
  30.    ** for a sphere with radius 5. */
  31.    C = cr_matrix( 4, 4 );
  32.    C[1][1] = C[2][2] = C[3][3] = 0.2;   C[0][3] = 2;
  33.    
  34.    /* Translate the sphere to x = 5, y = 5, z = 5 */
  35.    T[0] = T[1] = T[2] = -5.0;
  36.    srf_trans( C, T );
  37.  
  38.    #if defined( GRAPHICS_ON )
  39.    /* put screen in graphics mode. */
  40.    _setvideomode( _MRESNOCOLOR );
  41.    #endif
  42.  
  43.    /* Loop through some contours */
  44.    StpHAxis = ( MaxH - MinH ) / NmSteps;
  45.    StpVAxis = ( MaxV - MinV ) / NmSteps;
  46.    for ( Offset = 0.0; Offset < 5.0; Offset += 2.0 )
  47.       {
  48.       /* Loop through all the pixels. */
  49.       for ( i = 1, LocHAxis = MinH + StpHAxis,
  50.             LocVAxis = MinV + StpVAxis;
  51.             ( i <= NmSteps ) &&
  52.             ( LocHAxis <= MaxH ) &&
  53.             ( LocVAxis <= MaxV );
  54.             i++, LocHAxis += StpHAxis,
  55.             LocVAxis += StpVAxis )
  56.          {
  57.          /* Start a ray in the horizontal
  58.          ** direction going to the right.
  59.          ** Start a ray in the Vical
  60.          ** direction goting up. X is horizontal,
  61.          ** Y is the Vical, Z is normal. */
  62.          DirH[0] = DirV[1] = 1.0;
  63.          DirH[1] = DirH[2] = 0.0;
  64.          DirV[0] = DirV[2] = 0.0;
  65.          LocH[0] = MinH;
  66.          LocH[1] = LocVAxis;
  67.          LocV[0] = LocHAxis;
  68.          LocV[1] = MinV;
  69.          LocH[2] = LocV[2] = Offset;
  70.          NmIpH = srf_intrcpt( 2, LocH, DirH, C, IpH );
  71.          NmIpV = srf_intrcpt( 2, LocV, DirV, C, IpV );
  72.          for ( IpNm = 0; IpNm < NmIpH; IpNm++ )
  73.             {
  74.             Ip[0] = LocH[0] + IpH[IpNm];
  75.             Ip[1] = LocH[1];
  76.             #if defined( GRAPHICS_ON )
  77.             _setpixel( (short)( Ip[0] / MaxH *
  78.                   320.0 ), (short)( Ip[1] /
  79.                   MaxV * 200.0 ) );
  80.             #else
  81.             printf( "%g,\t%g,\t%g\n", Ip[0], Ip[1],
  82.                   Offset );
  83.             #endif
  84.             }
  85.          for ( IpNm = 0; IpNm < NmIpV; IpNm++ )
  86.             {
  87.             Ip[0] = LocV[0];
  88.             Ip[1] = LocV[1] + IpV[IpNm];
  89.             #if defined( GRAPHICS_ON )
  90.             _setpixel( (short)( Ip[0] / MaxH *
  91.                   320.0 ), (short)( Ip[1] /
  92.                   MaxV * 200.0 ) );
  93.             #else
  94.             printf( "%g,\t%g,\t%g\n", Ip[0], Ip[1],
  95.                   Offset );
  96.             #endif
  97.             }
  98.          }   /* for i */
  99.       }   /* for Offset */
  100.    fr_matrix( C, 4 );
  101.    }   /* function main */
  102. /* End of File */
  103.